package logic

import (
	"context"
	"fmt"
	"zhihu/models/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProblemShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProblemShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProblemShowLogic {
	return &ProblemShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 问题详情
func (l *ProblemShowLogic) ProblemShow(in *zhihurpc.ProblemShowRequest) (*zhihurpc.ProblemShowResponse, error) {
	// todo: add your logic here and delete this line
	var problems mysql.Problem
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&problems)
	if problems.ID == 0 {
		return nil, fmt.Errorf("当前问题不存在，无法查看详情")
	}

	problems.Hot = problems.Hot + 1
	mysql.DB.Debug().Updates(&problems)
	return &zhihurpc.ProblemShowResponse{
		Id:     int64(problems.ID),
		UserId: problems.UserId,
		Title:  problems.Title,
		Image:  problems.Image,
		Info:   problems.Info,
		Like:   problems.Like,
		Hot:    problems.Hot,
	}, nil
}
